// 格式化富文本
export function formatRichText(html) {
	let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
	　　match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
	　　match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
	　　match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
	　　return match;
	});
	newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
	　　match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
	　　return match;
	});
	newContent = newContent.replace(/<br[^>]*\/>/gi, '');
	newContent = newContent.replace(/\<img/gi,
	　　'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
	return newContent;
}

// 日期格式化
export function parseTime(time, pattern) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    } else if (typeof time === 'string') {
      time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.\d{3}/gm),'');
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}


/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
	uni.showToast({
		icon: 'none',
		title: content
	})
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: content,
			cancelText: '取消',
			confirmText: '确定',
			success: function(res) {
				resolve(res)
			}
		})
	})
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
	let result = ''
	for (const propName of Object.keys(params)) {
		const value = params[propName]
		var part = encodeURIComponent(propName) + "="
		if (value !== null && value !== "" && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
				for (const key of Object.keys(value)) {
					if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
						let params = propName + '[' + key + ']'
						var subPart = encodeURIComponent(params) + "="
						result += subPart + encodeURIComponent(value[key]) + "&"
					}
				}
			} else {
				result += part + encodeURIComponent(value) + "&"
			}
		}
	}
	return result
}

/**
 * 临时图片路径转base64
 * @param {Object} param 传一个对象，对象里包括
 * url: 临时图片存放路径，选择图片返回的相对路径
 * type: 图片类型，如：png
 */
function base64(url, type) {
	return new Promise((resolve, reject) => {
		if (!!uni.getFileSystemManager()) {
			uni.getFileSystemManager().readFile({
				filePath: url, //选择图片返回的相对路径
				encoding: "base64", //编码格式
				success: (res) => {
					resolve(
						"data:image/" +
						type.toLocaleLowerCase() +
						";base64," +
						res.data
					);
				},
				fail: (res) => reject(res.errMsg),
			});
		} else {
			uni.request({
				url: url,
				method: "GET",
				responseType: "arraybuffer",
				success: (ress) => {
					let base64 = wx.arrayBufferToBase64(ress.data); //把arraybuffer转成base64
					base64 = "data:image/jpeg;base64," + base64; //不加上这串字符，在页面无法显示的哦
					resolve(base64);
				},
				fail: (res) => reject(res.errMsg),
			});
		}
	});
}
